import Vue from 'vue'
import axios from 'axios'
import router from '@/router'
import { Message } from 'element-ui'
// import store from '@/store'
import { getToken, setToken } from '@/utils/auth'
Vue.prototype.moudules = {
  gateway: '/gateway',
  admin: '/admin',
  operate: '/operate',
  property: '/property',
  aided: '/aided',
  ysapi: '/ysapi'
}
// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
    const token = getToken()
    if (token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers['Authorization'] = 'Bearer ' + token
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
  */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  res => {
    if (res.config.isExcel) {
      return res
    }
    if (res.data instanceof Blob) {
      return res
    }
    if (res.data.respCode) {
      const respCode = res.data.respCode
      switch (respCode) {
        case '200':
          return res.data.data
        default:
          var respMsg = '服务器错误'
          if (res.data.respMsg && res.data.respMsg.length > 0) {
            respMsg = res.data.respMsg
          }
          Message.error(respMsg)
          return new Promise((resolve, reject) => {
            reject(res.data)
          })
      }
    } else {
      const respCode = res.data.code
      let respMsg = '服务器错误'
      switch (respCode) {
        case '200':
          return res.data
        default:
          if (res.data.msg && res.data.msg.length > 0) {
            respMsg = res.data.msg
          }
          Message({
            message: respMsg,
            type: 'error',
            duration: 5 * 1000
          })
          return new Promise((resolve, reject) => {
            reject(res.data)
          })
      }
    }
  },
  error => {
    const response = error.response
    if (!response) {
      Message.error('服务器太久没有响应, 请重试!')
    } else {
      const data = response.data
      const respCode = response.data.respCode
      switch (respCode) {
        case '400':
          break
        case '401':
          setToken('')
          Message.error(data.respMsg)
          router.push('/login')
          break
          /* router.replace('/401')
          break */
        case '403':
          setToken('')
          Message.error(data.respMsg)
          router.push('/login')
          break
        case '404':
          Message.error('服务器错误')
          break
        default:
          var respMsg = '服务器错误'
          if (response.data.respMsg && response.data.respMsg.length > 0) {
            respMsg = response.data.respMsg
          }
          Message.error(respMsg)
          break
      }
    }
    return Promise.reject(error.response)
  }
)

export default service
